﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Caching;
using System.Data.SqlClient;


namespace DataCaching
{
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObjectCache cache = MemoryCache.Default;
        string path = "C:\\FILE.txt";
        public MainWindow()
        {
            InitializeComponent();
            UpdateText();
        }

        private void UpdateText()
        {
          
            var content = cache["Contenido"] as string;
            if (content == null)
            {
                CacheItemPolicy p = new CacheItemPolicy();
                p.UpdateCallback = new CacheEntryUpdateCallback(CacheEntryUpdate);
                content = File.ReadAllText(path);
                HostFileChangeMonitor hfm = new HostFileChangeMonitor(new List<string>() { path });
                //hfm.NotifyOnChanged(new OnChangedCallback((o) =>
                //{
                //    content = File.ReadAllText(path);

                    
                //    cache["Contenido"] = content;

                //}));
                p.ChangeMonitors.Add(hfm);
                cache.Set("Contenido", content, p);
            }
            txt.Text = content;
        }



        private void UpdateTextSQL() 
        {

            var content = cache["ContenidoSQL"] as string;
            if (content == null)
            {
               SqlConnection conn = new SqlConnection(@"Data Source=BQTO6\GALACSQLX08;Initial Catalog=ADMECDB_OFICIAL;Integrated Security=True");
               SqlDependency.Start(@"Data Source=BQTO6\GALACSQLX08;Initial Catalog=ADMECDB_OFICIAL;Integrated Security=True");
               SqlCommand cmm = new SqlCommand("Select Count_Big(*) AS X from Saw.Almacen",conn); 
                CacheItemPolicy p = new CacheItemPolicy();
                SqlDependency sqld = new SqlDependency(cmm);
                sqld.OnChange += new OnChangeEventHandler(sqld_OnChange);
                SqlChangeMonitor hfm = new SqlChangeMonitor(sqld);
                //hfm.NotifyOnChanged(new OnChangedCallback((o) =>
                //{
                //    content = File.ReadAllText(path);


                //    cache["Contenido"] = content;

                //}));
                conn.Open();
                content = cmm.ExecuteScalar().ToString();
                p.ChangeMonitors.Add(hfm);
                // cache.Set("ContenidoSQL", content, new CacheItemPolicy());
                cache.Set("ContenidoSQL", content, p);

            }
            txt.Text = content;
        }

        void sqld_OnChange(object sender, SqlNotificationEventArgs e)
        {
            MessageBox.Show("iii");


        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UpdateText();
        }

        private void CacheEntryUpdate(CacheEntryUpdateArguments args)
        {
           // var cacheItem = cache.GetCacheItem(args.Key);
           // var cacheObj = cacheItem.Value;
           //cacheObj= File.ReadAllText(path);
           
            

           // cacheItem.Value = cacheObj;
           // args.UpdatedCacheItem = cacheItem;
           // var policy = new CacheItemPolicy
           // {
           //     UpdateCallback = new CacheEntryUpdateCallback(CacheEntryUpdate),
           //     AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(10)
           // };
           // args.UpdatedCacheItemPolicy = policy;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            UpdateTextSQL();
        }
    }
}
